<?php 

namespace Admin\Controller;

use Admin\Controller\CommonController;

class AskController extends CommonController
{
	// 问题列表页
	public function index()
	{
		// 问题分类
		$cats=D('AskCategory')->getTreeData('tree','order_number desc,id');
		$map = [
			"cid" => I("get.cid", ""),
		];
		$asks = D('Ask')->getPage(M("ask"), "", "clickgood_num desc, view_num desc");
		$this->assign("data", $asks["data"]);
		$this->assign("page", $asks["page"]);
		$this->assign("cats", $cats);
		$this->assign("cid", I("get.cid", ""));
		$this->display();
	}


	public function add()
	{
		// 问题分类
		$cats=D('AskCategory')->getTreeData('tree','order_number desc,id');	
		$this->assign("cats", $cats);
		$this->display();
	}

	public function doAdd()
	{
		// 用户的id
		// 分类的id
		// 发布时间
		// status  1审核 2发布  345 
		if ( empty(I("post.cid")) ) {
			return show(0, "请选择分类");
		}
		if ( empty(I("post.title")) ) {
			return show(0, "缺少内容描述");
		}
		// dump(I("post.", "htmlspecialchars"));
		$data = I("post.", "htmlspecialchars");
		$data["public_time"] = time(); 
		$data["status"] = 2;
		$data["uid"] = $_SESSION["userinfo"]["id"];

		// dump($data);die;

		$res = D("Ask")->addData($data);
		if ($res) {
			return show(1, "添加成功");
		}
		return show(0, "添加失败，请稍后添加");
	}

	public function edit()
	{
		// 问题分类
		$cats=D('AskCategory')->getTreeData('tree','order_number desc,id');	
		$data = D("Ask")->getData([
			"id" => I("id")
		]);

		// dump($data);
		$this->assign("cats", $cats);
		$this->assign("data", $data);
		$this->display();
	}

	public function doEdit()
	{
		$id = I("post.id");
		$data = I("post.");
		unset($data['id']);
		$data["last_time"] = time();
		$res = D("Ask")->editData([
			"id" => $id
		], $data);
		if ($res) {
			return show(1, "修改成功");
		}
		return show(0, "修改失败");
	}


	public function del()
	{

	}

}